#include <string>
#include <Util/VoiceDataTranster.h>

enum MsgType
{
    TYPE_NONE = 0,
    TYPE_VOICE,
    TYPE_PLAY,
    TYPE_PAUSE,
    TYPE_HANDSHAKE,
    TYPE_HEART,
    TYPE_SERVER_ALIVE
};
class VoiceProtocol
{
public :

    /// <summary>
    /// 解析消息类型
    /// </summary>
    /// <param name="data"></param>
    /// <param name="type"></param>
    /// <param name="id"></param>
    /// <returns></returns>
    static uint8_t* UnPackData(uint8_t* data,  MsgType& type, int& id)
    {
        int data_len = strlen((const char*)data);
        if (data_len != 326)
            return null;
        uint8_t* out_data = new uint8_t[data_len - 6];
        type = (MsgType)Byte2Short(data);
        id = Byte2Int(data);
       // Array.Copy(data, 6, out_data, 0, strlen(out_data));
        memcpy(out_data, data+6, strlen((const char*)out_data));
        return out_data;
    }


    /// <summary>
    /// | type | voice |
    ///    2       N
    /// </summary>
    /// <param name="id"></param>
    /// <param name="data"></param>
    /// <returns></returns>
    static uint8_t* PackVoice(uint8_t* data)
    {
        return PackData(MsgType.TYPE_VOICE, data);
    }

    /// <summary>
    /// | type | otherids|
    ///     2     4b4b4b
    /// </summary>
    /// <param name="id"></param>
    /// <param name="otherid"></param>
    /// <returns></returns>
    static uint8_t* PackPlayMsg(int* otherid, int len)
    {
        int data_len = len * 4;
        uint8_t* data = new uint8_t[data_len];
        int index = 0;
        for (int i = 0; i < len; ++i)
        {
            //Array.Copy(VoiceDataTransfer.Int322Byte(otherid[i]), 0, data, index, sizeof(int));
            uint8_t temp[4];
            Int322Byte(otherid[i], temp);
            memcpy(data+index, temp, 4);
            index += 4;
        }
        return PackData(MsgType.TYPE_PLAY, data);
    }

    /// <summary>
    /// | type | otherids|
    ///     2     4b4b4b
    /// </summary>
    /// <param name="id"></param>
    /// <param name="otherid"></param>
    /// <returns></returns>
    static uint8_t* PackPauseMsg(int* otherid)
    {
        int data_len = otherid.Length * 4;
        uint8_t* data = new uint8_t[data_len];
        int index = 0;
        for (int i = 0; i < otherid.Length; ++i)
        {
            //Array.Copy(VoiceDataTransfer.Int2Byte(otherid[i]), 0, data, index, sizeof(int));
            uint8_t temp[4];
            Int322Byte(otherid[i], temp);
            memcpy(data+index, temp, 4);
            index += 4;
        }
        return PackData(MsgType.TYPE_PAUSE, data);
    }

    /// <summary>
    /// | type | id | roomid|
    ///    2      4     4
    /// </summary>
    /// <param name="id"></param>
    /// <param name="roomid"></param>
    /// <returns></returns>
    static uint8_t* PackHandshakeMsg(int id, int roomid)
    {
        uint8_t* data = new uint8_t[4 + 4];
        //  Array.Copy(VoiceDataTransfer.Int2Byte(roomid), 0, data, 0, sizeof(int));
        //Array.Copy(VoiceDataTransfer.Int2Byte(id), 0, data, 4, sizeof(int));
        uint8_t temp[4];
        Int322Byte(roomid, temp);
        memcpy(data, temp, 4);
        Int322Byte(id, temp);
        memcpy(data+4, temp, 4);
        return PackData(MsgType.TYPE_HANDSHAKE, data);
    }


    static uint8_t* PackHeartMsg()
    {
        return PackData(MsgType.TYPE_HEART);
    }



private:
    /// <summary>
    /// 封装消息类型
    /// | type | msg|
    /// </summary>
    /// <param name="type"></param>
    /// <param name="data"></param>
    /// <returns></returns>
    static uint8_t* PackData(MsgType type, uint8_t* data = null)
    {
        uint8_t* out_data = null;
        if (data != null)
        {
            out_data = new uint8_t[2 +strlen((const char*)data)];
        }
        else
            out_data = new uint8_t[2];

       // Array.Copy(VoiceDataTransfer.Short2Byte((short)type), 0, out_data, 0, sizeof(short));
        uint8_t temp[2];
        Short2Byte((short)type, temp);
        memcpy(out_data, temp, sizeof(short));
        if (null != data)
        {
          //  Array.Copy(data, 0, out_data, 2, data.Length);
            memcpy(out_data+2, data, strlen((const char*)data));
        }
        return out_data;
    }

}
